using CharacterCreation.Data.Models;
using CharacterCreation.Data.Services;
using Microsoft.Practices.Prism.ViewModel;
using CharacterCreation.Views.Contracts;
using CharacterCreation.Views.ChildWindows;
using System.Windows.Documents;

namespace CharacterCreation.Views {
    /// <summary>
    /// The host view model.
    /// </summary>
    public partial class CharacterHostViewModel : BaseHostViewModel {

        private CharacterService _characterService = CharacterService.CreateInstance();

        /// <summary>
        /// The currently selected character
        /// </summary>
        public Character Character {
            get { return _characterService.CurrentItem; }
        }

        /// <summary>
        /// Character document
        /// </summary>
        public FlowDocument CharacterDocument {
            get { return _characterService.Document; }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public CharacterHostViewModel() {
            _characterService.PropertyChanged += (s, e) => {
                if (e.PropertyName == "CurrentItem") {
                    RaisePropertyChanged("Character");
                    RaisePropertyChanged("CharacterDocument");
                }
            };
        }

        /// <summary>
        /// Shows the add popup to create and config a new character.
        /// </summary>
        public override void Add() {
            _characterService.ClearNewItem();
            PopupWindow popupWindow = new PopupWindow(
                new BaseView[] { new CharacterNewView(), new CharacterNewStatisticsView(), new CharacterNewSkillsView(), new CharacterNewSpecialView() }
            );
            popupWindow.Closing += (s, e) => {
                if (popupWindow.DialogResult != null && (bool)popupWindow.DialogResult) {
                    _characterService.SaveNewItem();
                }
            };
            popupWindow.ShowDialog();
        }

        /// <summary>
        /// Deletes a character
        /// </summary>
        public override void Delete() {
            _characterService.DeleteCurrentItem();
        }

        /// <summary>
        /// Shows a popup to edit the character
        /// </summary>
        public override void Edit() {
            if (_characterService.CurrentItem == null) return;

            PopupWindow popupWindow = new PopupWindow(
                new BaseView[] { new CharacterEditView(), new CharacterEditStatisticsView(), new CharacterEditSkillsView(), new CharacterEditSpecialsView() }
            );
            popupWindow.Closing += (s, e) => {
                if (popupWindow.DialogResult != null && (bool)popupWindow.DialogResult) {
                    _characterService.SaveCurrentItem();
                    RaisePropertyChanged("CharacterDocument");
                }
            };
            popupWindow.ShowDialog();
        }


        /// <summary>
        /// Saves a character
        /// </summary>
        public override void Save() {
            _characterService.SaveCurrentItem();
        }

        /// <summary>
        /// Exports the current characters
        /// </summary>
        public override void Export() {
            _characterService.Export();
        }
    }
}